lib: Fix some logic/error-checking code
authorJonathan Lebon <jonathan@jlebon.com>
Wed, 25 Jul 2018 21:51:01 +0000 (17:51 -0400)
committerAtomic Bot <atomic-devel@projectatomic.io>
Thu, 26 Jul 2018 21:01:19 +0000 (21:01 +0000)
Using `MAX(0, $x)` here is useless since we're comparing against an
unsigned integer. Just unpack this and only subtract if it's safe to do
so.

Also, explicitly check for `fd >= 0` rather than just `!= -1` to be sure
it's a valid fd. And finally, explicitly check the return value of
`g_input_stream_read_all` as is done everywhere else in the tree and
make it clear that we're purposely ignoring the return value of `_flush`
here, but not in other places.

Discovered by Coverity.

Closes: #1692
Approved by: cgwalters

src/libostree/ostree-repo.c
src/libotutil/ot-gpg-utils.c

index f4dcb703fc1f3146fe11634181987941da16b5eb..92148b031fdeb721f88fdb100018145ff8a472c0 100644 (file)
@@ -4611,8 +4611,9 @@ ostree_repo_pull_default_console_progress_changed (OstreeAsyncProgress *progress
 
           if (bytes_sec > 0)
             {
-              /* MAX(0, value) here just to be defensive */
-              guint64 est_time_remaining = MAX(0, (total_delta_part_size - fetched_delta_part_size)) / bytes_sec;
+              guint64 est_time_remaining = 0;
+              if (total_delta_part_size > fetched_delta_part_size)
+                est_time_remaining = (total_delta_part_size - fetched_delta_part_size) / bytes_sec;
               g_autofree char *formatted_est_time_remaining = _formatted_time_remaining_from_seconds (est_time_remaining);
               /* No space between %s and remaining, since formatted_est_time_remaining has a trailing space */
               g_string_append_printf (buf, "Receiving delta parts: %u/%u %s/%s %s/s %sremaining",
@@ -4891,7 +4892,7 @@ ostree_repo_add_gpg_signature_summary (OstreeRepo     *self,
   g_autoptr(GVariant) metadata = NULL;
   if (!ot_openat_ignore_enoent (self->repo_dir_fd, "summary.sig", &fd, error))
     return FALSE;
-  if (fd != -1)
+  if (fd >= 0)
     {
       if (!ot_variant_read_fd (fd, 0, G_VARIANT_TYPE (OSTREE_SUMMARY_SIG_GVARIANT_STRING),
                                FALSE, &metadata, error))
index 9d5c8d3ab32ad0bedf946467a9b059166320c8bf..cc5b0ae4e1de172efe00514335e8825452c9ca0d 100644 (file)
@@ -262,10 +262,8 @@ data_read_cb (void *handle, void *buffer, size_t size)
 
   g_return_val_if_fail (G_IS_INPUT_STREAM (input_stream), -1);
 
-  g_input_stream_read_all (input_stream, buffer, size,
-                           &bytes_read, NULL, &local_error);
-
-  if (local_error != NULL)
+  if (!g_input_stream_read_all (input_stream, buffer, size,
+                                &bytes_read, NULL, &local_error))
     {
       set_errno_from_gio_error (local_error);
       g_clear_error (&local_error);
@@ -287,7 +285,7 @@ data_write_cb (void *handle, const void *buffer, size_t size)
   if (g_output_stream_write_all (output_stream, buffer, size,
                                  &bytes_written, NULL, &local_error))
     {
-      g_output_stream_flush (output_stream, NULL, &local_error);
+      (void)g_output_stream_flush (output_stream, NULL, &local_error);
     }
 
   if (local_error != NULL)